﻿using Abp.Dependency;
using Mt.Site.Core.Sql;
using System;

namespace Mt.Site.Core.StaticPages.BackGroundJobs
{
    public class HangfireApiHelper : IHangfireApiHelper, ISingletonDependency
    {
        //private readonly IAppConfigurationAccessor _appConfigurationAccessor;
        private readonly ISqlExecuter _sqlExecuter;

        public HangfireApiHelper(ISqlExecuter sqlExecuter)
        {
            _sqlExecuter = sqlExecuter;
        }

        public int GetEnqueuedJobCount(string jobName)
        {
            var result = _sqlExecuter.GetSingleValue($"SELECT  count(*) FROM [HangFire].[Job] where stateName='Enqueued' and InvocationData like '%{jobName}%'");

            //var result = _sqlExecuter.GetSingleValue($"SELECT  count(*) FROM HangFireJob where stateName='Enqueued' and InvocationData like '%{jobName}%'");
            if(result == null)
            {
                return 0;
            }
            return Convert.ToInt32(result);
        }
    }
}
